import pymysql


def main():
    no = int(input('要编辑的学生编号:'))
    loc = input('学生的新地址:')
    # 1创建连接对象
    conn = pymysql.connect(host='8.147.131.121', port=3306,
                           user='root1', password='123456',
                           database='school', charset='utf8')
    # 上下文语法，离开这个语法就自动关闭
    try:
        # 2获得游标对象
        with conn.cursor() as cursor:
            # 3执行SQL得到结果
            result = cursor.execute('update tb_student set stuaddr=%s where stuid=%s;',
                                    (loc, no))
            if result == 1:
                print('更新成功!')
            # 4操作成功执行提交
            conn.commit()
    except pymysql.MySQLError as error:
        print(error)
        # 4操作失败执行回滚
        conn.rollback()
    finally:
        # 5关闭连接释放资源
        conn.close()
    print(conn)


if __name__ == '__main__':
    main()
